package com.hui.mapper;

import com.hui.dto.RolePageDTO;
import com.hui.dto.RoomPageDTO;
import com.hui.entity.EmpRole;
import com.hui.entity.Role;
import com.hui.entity.Room;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;


public interface RoleMapper {
    @Insert("""
            insert into ums_role(title, info, version, deleted, created, updated)
            values (#{title},#{info}, #{version}, #{deleted}, #{created}, #{updated})
            """)
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert (Role role);

    @Select("""
            select * from ums_role t
            where t.id = #{param1} and t.deleted = 0
            """)
    Role select(Long id);

    @Select("""
            <script>
            select * from ums_role t
            <where>
            <if test='title != null'> title like concat('%' , #{title}, '%') and </if>
            t.deleted = 0
            </where>
            </script>
            """)
    List<Role> list(RolePageDTO rolePageDTO);

    @Update("""
            <script>
            update ums_role
            <set>
            <if test='title != null'> title = #{title},</if>
            <if test='info != null'> info = #{info},</if>
            <if test='updated != null'> updated = #{updated},</if>
            <if test='deleted != null'> deleted = #{deleted},</if>
            <if test='version != null'> version = #{version},</if>
            version = version + 1
            </set>
            where id = #{id} and deleted = 0 and version = #{version} 
            </script>
            """)
    int update(Role role);

    @Update("""
            update ums_role
            set deleted = 1, updated = current_timestamp
            where id = #{param1} 
            """)
    int delete(Long id);

    @Update("""
            <script>
            update ums_role set deleted = 1, updated = current_timestamp
            where id in
            <foreach collection='list'  item='e'  open='(' close=')' separator=','>
            #{e}
            </foreach>
            </script>
            """)
    int deleteBatch(List<Long> ids);

    @Select("""
            select * from ums_role t2 where id in (
                       select fk_role_id from ums_emp_role t1\s
                       where t1.fk_emp_id = #{param1} and t1.deleted = 0
                       )and t2.deleted = 0
            """)
    List<Role> listByEmpId(Long empId);


    @Update("""
		update ums_emp_role set deleted = 1, updated = now() where fk_emp_id = #{param1}
		""")
    int deleteEmpRoleByEmpId(Long empId);
    @Insert("""
		insert into ums_emp_role (fk_emp_id, fk_role_id, version, deleted, created, updated)
		values (#{fkEmpId}, #{fkRoleId}, #{version}, #{deleted}, #{created}, #{updated})
		""")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertEmpRole(EmpRole empRole);




}
